#ifndef FILE_READER_H
#define FILE_READER_H

#include "Prop.h"

#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
#include <cstdlib>

using namespace std;

vector<string> TokenizeLine(string line, string delimiters) {
    vector<string> output;    
    bool shouldBreak = false;
    string builtString ("");

    for (int i = 0; i < line.size(); i++) {
        for (int j = 0; j < delimiters.size(); j++) {
            if (line[i] == delimiters[j]) {
                shouldBreak = true;
            }
        }

        if (shouldBreak) {
            if (builtString.size() > 0) {
                output.push_back(builtString);
            }            
            builtString.clear();
            shouldBreak = false;
        }
        else {
            builtString = builtString + line[i];
        }
    }

    if (builtString.size() > 0) {
        output.push_back(builtString);
    }

    return output;
}

vector<Prop> ReadLevel(const char* name, RenderManager rManager) {
    vector<Prop> level;
    vector<string> list;
    string line;
    char ch;
    int nestedBraces = 0;

    ifstream myfile(name);

    if (myfile.is_open()) {
        while(!myfile.eof()) {
	        myfile.get(ch);

	        if (ch == '\n' || ch == '\r') {
	            if (line[0] == '/' && line[1] == '/') {
	                cout << "Comment found, throwing line away..." << endl;
	                line = "";
	            }
	        }
	        else if (ch == '{') {
	            nestedBraces++;
	        }
	        else if (ch == '}') {
	            nestedBraces--;
	            
	            // If the nested braces are ended, process the data elements
	            if (nestedBraces == 0) {
	                list = TokenizeLine(line, " <>,");
	                
                    if (list[0] == "player") {
                        
                    }
                    else {
                        Prop p = Prop(list);
	                    p.SetModelData(rManager);
	                    level.push_back(p);
                    }
	                
	                line = "";
	            }
	        }
	        else {
	            line = line + ch;
	        }
	    }
    }
    else {
        printf("Error opening file\n");
    }
    
    printf("Read in %d props\n", level.size());
    
    return level;
}

#endif
